package com.fdd.furns.web;

import com.fdd.furns.entity.Furn;
import com.fdd.furns.entity.Page;
import com.fdd.furns.service.FurnService;
import com.fdd.furns.service.impl.FurnServiceImpl;
import com.fdd.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/customerFurnServlet")
public class CustomerFurnServlet extends BasicServlet {

    private FurnService furnService = new FurnServiceImpl();

    /**
     * 这里是一个分页请求家居信息的api/方法
     *
     * @param req
     * @param resp /**
     *             这里仍然是一个分页请求家居信息的api/方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("pageByName被调用了");
        //这里的业务逻辑与后台分页显示家居信息的逻辑是一样的
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = req.getParameter("name");

        if(null == name){//name字段不传,就给一个空字符串
            name = "";
        }


        //获取page对象
        Page<Furn> page = furnService.pageByName(pageNo, pageSize, name);

        //设置url
        StringBuilder url = new StringBuilder("customerFurnServlet?action=pageByName");
        if (!name.equals("")) {
            url.append("&name=").append(name);//如果name不为空，说明是通过name查询的，翻页的时候需要将name带上去
        }
        page.setUrl(url.toString());


        //将page对象放入request域中
        req.setAttribute("page", page);


        //请求转发到furn_manage.jsp
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }

}
